package com.dongdongkeji.base.widget.progress;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;

import com.dongdongkeji.base.widget.dialog.ProgressDialog;

/**
 * Created by CJ on 2016/10/27 0027.
 */
public class ProgressDialogHandler extends Handler {

    public static final int SHOW_PROGRESS_DIALOG = 1;
    public static final int DISMISS_PROGRESS_DIALOG = 2;

    private ProgressDialog progressDialog;

    private Context mContext;

    private boolean cancelable;

    private DialogInterface.OnCancelListener progressCancelListener;

    public ProgressDialogHandler(Context mContext, @NonNull DialogInterface.OnCancelListener onCancelListener, boolean cancelable) {
        super();
        this.progressCancelListener = onCancelListener;
        this.mContext = mContext;
        this.cancelable = cancelable;
    }

    private void initProgressDialog(){
        if (progressDialog == null){
            progressDialog = new ProgressDialog(mContext);
            progressDialog.setCancelable(cancelable);
            if (cancelable){
                progressDialog.setOnCancelListener(progressCancelListener);
            }
        }
        if (!progressDialog.isShowing()){
            progressDialog.show();
        }
    }

    private void dismissProgressDialog(){
        if (progressDialog != null){
            progressDialog.dismiss();
            progressDialog = null;
        }
    }

    @Override
    public void handleMessage(Message msg) {
        switch (msg.what){
            case SHOW_PROGRESS_DIALOG:
                initProgressDialog();
                break;
            case DISMISS_PROGRESS_DIALOG:
                dismissProgressDialog();
                break;
        }
    }
}
